package view;

import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.JButton;

import dao.LieferantDAO;
import dao.WarengruppeDAO;
import model.Lieferant;
import model.Warengruppe;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class WarengruppenPanel extends JPanel {
	private JLabel lblLieferantenverwaltung;
	private JLabel lblId;
	private JTextField txtID;
	private JButton btnSuchen;
	private JLabel lblName;
	private JTextField txtName;
	private JButton btnAnlegen;
	private JButton btnndern;
	private JButton btnLschen;

	/**
	 * Create the panel.
	 */
	public WarengruppenPanel() {
		setLayout(null);
		
		this.lblLieferantenverwaltung = new JLabel("Warengruppenverwaltung");
		this.lblLieferantenverwaltung.setFont(new Font("Tahoma", Font.BOLD, 17));
		this.lblLieferantenverwaltung.setBounds(25, 21, 299, 31);
		add(this.lblLieferantenverwaltung);
		
		this.lblId = new JLabel("ID:");
		this.lblId.setBounds(25, 85, 46, 14);
		add(this.lblId);
		
		this.txtID = new JTextField();
		this.txtID.setBounds(79, 82, 86, 20);
		add(this.txtID);
		this.txtID.setColumns(10);
		
		this.btnSuchen = new JButton("suchen");
		this.btnSuchen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				do_btnSuchen_actionPerformed(e);
			}
		});
		this.btnSuchen.setBounds(176, 81, 89, 23);
		add(this.btnSuchen);
		
		this.lblName = new JLabel("Name:");
		this.lblName.setBounds(25, 115, 46, 14);
		add(this.lblName);
		
		this.txtName = new JTextField();
		this.txtName.setBounds(79, 112, 86, 20);
		add(this.txtName);
		this.txtName.setColumns(10);
		
		this.btnAnlegen = new JButton("anlegen");
		this.btnAnlegen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				do_btnAnlegen_actionPerformed(arg0);
			}
		});
		this.btnAnlegen.setBounds(25, 166, 89, 23);
		add(this.btnAnlegen);
		
		this.btnndern = new JButton("\u00E4ndern");
		this.btnndern.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				do_btnndern_actionPerformed(e);
			}
		});
		this.btnndern.setBounds(124, 166, 89, 23);
		add(this.btnndern);
		
		this.btnLschen = new JButton("l\u00F6schen");
		this.btnLschen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				do_btnLschen_actionPerformed(e);
			}
		});
		this.btnLschen.setBounds(223, 166, 89, 23);
		add(this.btnLschen);

	}
	protected void do_btnAnlegen_actionPerformed(ActionEvent arg0) {
		Warengruppe l=new Warengruppe();
		l.setId(Integer.parseInt(txtID.getText()));
		l.setName(txtName.getText());
		WarengruppeDAO.save(l);
	}
	protected void do_btnSuchen_actionPerformed(ActionEvent e) {
		Warengruppe l=WarengruppeDAO.get(Integer.parseInt(txtID.getText()));
		txtName.setText(l.getName());
	}
	protected void do_btnLschen_actionPerformed(ActionEvent e) {
		WarengruppeDAO.delete(Integer.parseInt(txtID.getText()));
	}
	protected void do_btnndern_actionPerformed(ActionEvent e) {
		Warengruppe l=new Warengruppe();
		l.setId(Integer.parseInt(txtID.getText()));
		l.setName(txtName.getText());
		WarengruppeDAO.update(l);
	}
}
